package com.example.backend.service.mysql.user.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.backend.dto.request.AddressDto;
import com.example.backend.dto.request.PageUserDto;
import com.example.backend.entitly.mysql.user.UserAddresses;
import com.example.backend.mapper.mysql.user.UserAddressesMapper;
import com.example.backend.service.mysql.user.UserAddressesService;
import org.springframework.stereotype.Service;

/**
 * @author 1
 * @description 针对表【user_addresses(用户地址表)】的数据库操作Service实现
 * @createDate 2025-06-04 09:01:52
 */
@Service
public class UserAddressesServiceImpl extends ServiceImpl<UserAddressesMapper, UserAddresses>
        implements UserAddressesService {

    @Override
    public Page<UserAddresses> getAddressList(PageUserDto pageUserDto) {
        Page<UserAddresses> objectPage = new Page<>(pageUserDto.getPageNum(), pageUserDto.getPageSize());
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(UserAddresses::getUserId, pageUserDto.getUserId())
                .page(objectPage);
    }

    @Override
    public boolean saveByAddress(AddressDto addressDto) {
        if (!countByAddress(addressDto.getUserId())) {
            addressDto.setIsDefault(1);
        } else if (addressDto.getIsDefault() != null && addressDto.getIsDefault().equals(1)) {
            new LambdaUpdateChainWrapper<>(getBaseMapper())
                    .eq(UserAddresses::getUserId, addressDto.getUserId())
                    .eq(UserAddresses::getIsDefault, 1)
                    .set(UserAddresses::getIsDefault, 0)
                    .update();
        }
        UserAddresses convert = Convert.convert(UserAddresses.class, addressDto);
        return save(convert);
    }

    @Override
    public UserAddresses getByDefault(Integer userId) {
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(UserAddresses::getUserId, userId)
                .eq(UserAddresses::getIsDefault, 1)
                .one();
    }

    @Override
    public boolean countByAddress(Integer userId) {
        Long count = new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(UserAddresses::getUserId, userId)
                .count();

        return count > 0;
    }

    @Override
    public boolean removeByAddress(Integer addressId, Integer userId) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(UserAddresses::getAddressId, addressId)
                .eq(UserAddresses::getUserId, userId)
                .remove();
    }

    @Override
    public boolean updateByAddress(AddressDto addressDto) {
        UserAddresses userAddresses = Convert.convert(UserAddresses.class, addressDto);
        return saveOrUpdate(userAddresses);
    }
}




